﻿using System;
using System.Collections.Generic;
using System.Data;
using ContractManager.Common.Class;
using ContractManager.Entity;
using Speed.Data;

namespace ContractManager.BLL.Business
{
    public static class ClsForms
    {
        public static List<Forms> SelectAll()
        {
            using (var db = Sys.NewDb())
            {
                return Data.Base.Forms.Select(db);
            }
        }

        public static DataTable GetAll()
        {
            using (var db = Sys.NewDb())
            {
                return db.ExecuteDataTable("EXEC dbo.usp_FormSelectAll");
            }
            
        }

        public static DataTable GetFormActivate()
        {
            using (var db = Sys.NewDb())
            {
                return db.ExecuteDataTable("EXEC dbo.usp_FormSelectActivate");
            }

        }

        public static DataTable SelectLeafNode()
        {
            using (var db = Sys.NewDb())
            {
                return db.ExecuteDataTable("EXEC dbo.usp_AS_Acc_AccountSelectLeafNode");
            }
            
        }

        public static string GetVoucherLockListDate(int formNo)
        {
            string commandText = String.Format("EXEC dbo.usp_VoucherLockListDate {0},{1} ", ClsVariable.AsSysMyCoYearId, formNo);

            using (var db = Sys.NewDb())
            {
                return db.ExecuteString(commandText);
            }
        }

        public static DataTable SelectMenuByRole(int role)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_RoleFormGetByRoleNo {0}", role);
                return db.ExecuteDataTable(commandText);
            }
            
        }

        public static RoleForms SelectByUserForm(int role, int formNo)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("RoleNo={0} AND FormNo={1}", role, formNo);
                return db.SelectSingle<RoleForms>(commandText);
            }
            
        }

        public static void Insert(Forms capital)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Insert<Forms>(capital, EnumSaveMode.Requery);
                string commandText = String.Format("EXEC dbo.usp_FormUpdateSortOrder {0},{1}", capital.FormNo, capital.Form_Parent ?? 0);
                db.ExecuteNonQuery(commandText);
                
                db.Commit();
            }
            
        }

        public static void Update(Forms capital)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Update<Forms>(capital);
                string commandText = String.Format("EXEC dbo.usp_FormUpdateSortOrder {0},{1}", capital.FormNo, capital.Form_Parent ?? 0);
                db.ExecuteNonQuery(commandText);
                
                db.Commit();
            }
            
        }

        public static void UpdateNodeId(int formNo, string nodeId)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_FormUpdateNodeId {0},'{1}'", formNo, nodeId);
                db.ExecuteNonQuery(commandText);
            }
            
        }

        public static void UpdateSortOrder(int formNo, int? formParent)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_FormUpdateSortOrder {0},{1}", formNo, formParent);
                db.ExecuteNonQuery(commandText);
            }
            
        }

        public static Forms SelectByPk(Int32 formNo)
        {
            using (var db = Sys.NewDb())
            {
                return db.SelectSingle<Forms>(string.Format("FormNo={0}", formNo));
            }
            
        }

        public static List<Forms> SelectFormOpen()
        {
            using (var db = Sys.NewDb())
            {
                return db.Select<Forms>(string.Format("Form_IsMainOpen={0}", 1));
            }

        }

        public static Forms SelectByFormClass(string formClass)
        {
            using (var db = Sys.NewDb())
            {
                return db.SelectSingle<Forms>(string.Format("Form_Class='{0}'", formClass));
            }
            
        }

        public static int DeleteByPk(Int32 formNo)
        {
            using (var db = Sys.NewDb())
            {
                return db.Delete<Forms>(string.Format("FormNo={0}", formNo));
            }
            
        }


        public static DataTable SelectByUserMenu(Int32 employeeId)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_FormSelectByUser {0}", employeeId);
                return db.ExecuteDataTable(commandText);
            }
            
        }
    }
}
